<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="文件名称" prop="fileName">
            <el-input
               v-model="queryParams.fileName"
               placeholder="请输入文件名称"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="文件类型" prop="fileType">
            <el-select v-model="queryParams.fileType" placeholder="文件类型" clearable style="width: 200px">
               <el-option label="图片" value="image" />
               <el-option label="视频" value="video" />
               <el-option label="文档" value="document" />
            </el-select>
         </el-form-item>
         <el-form-item label="设备编号" prop="deviceCode">
            <el-input
               v-model="queryParams.deviceCode"
               placeholder="请输入设备编号"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Download"
               @click="handleDownload"
               v-hasPermi="['data:file:download']"
            >批量下载</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
               v-hasPermi="['data:file:export']"
            >导出</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="fileList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column type="index" label="序号" width="60" align="center" />
         <el-table-column label="文件名称" align="center" prop="fileName" :show-overflow-tooltip="true" />
         <el-table-column label="文件类型" align="center" prop="fileType" :show-overflow-tooltip="true">
            <template #default="scope">
               <el-tag v-if="scope.row.fileType === 'image'" type="success">图片</el-tag>
               <el-tag v-else-if="scope.row.fileType === 'video'" type="warning">视频</el-tag>
               <el-tag v-else type="info">文档</el-tag>
            </template>
         </el-table-column>
         <el-table-column label="文件大小" align="center" prop="fileSize" :show-overflow-tooltip="true">
            <template #default="scope">
               <span>{{ formatFileSize(scope.row.fileSize) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="设备编号" align="center" prop="deviceCode" :show-overflow-tooltip="true" />
         <el-table-column label="上传时间" align="center" prop="uploadTime" width="180">
            <template #default="scope">
               <span>{{ parseTime(scope.row.uploadTime) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['data:file:query']">查看</el-button>
               <el-button link type="primary" icon="Download" @click="handleDownloadFile(scope.row)" v-hasPermi="['data:file:download']">下载</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />
   </div>
</template>

<script setup name="File">
import { listFile, downloadFile, batchDownloadFile } from "@/api/data/file";

const { proxy } = getCurrentInstance();

const fileList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const multiple = ref(true);
const total = ref(0);

const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  fileName: undefined,
  fileType: undefined,
  deviceCode: undefined
});

/** 查询文件列表 */
function getList() {
  loading.value = true;
  // TODO: 调用实际API
  // listFile(queryParams.value).then(response => {
  //   fileList.value = response.rows;
  //   total.value = response.total;
  //   loading.value = false;
  // });
  
  setTimeout(() => {
    fileList.value = [];
    total.value = 0;
    loading.value = false;
    proxy.$modal.msgWarning("文件获取功能开发中，请等待后端接口...");
  }, 500);
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.fileId);
  multiple.value = !selection.length;
}

/** 查看文件 */
function handleView(row) {
  // TODO: 打开文件预览
  proxy.$modal.msgWarning("文件预览功能开发中...");
}

/** 下载单个文件 */
function handleDownloadFile(row) {
  // TODO: 下载文件
  proxy.$modal.msgWarning("文件下载功能开发中...");
}

/** 批量下载 */
function handleDownload() {
  if (ids.value.length === 0) {
    proxy.$modal.msgWarning("请选择要下载的文件");
    return;
  }
  // TODO: 批量下载
  proxy.$modal.msgWarning("批量下载功能开发中...");
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download("data/file/export", {
    ...queryParams.value
  }, `file_${new Date().getTime()}.xlsx`);
}

/** 格式化文件大小 */
function formatFileSize(bytes) {
  if (!bytes) return '0 B';
  const k = 1024;
  const sizes = ['B', 'KB', 'MB', 'GB'];
  const i = Math.floor(Math.log(bytes) / Math.log(k));
  return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}

getList();
</script>

